import axios from "@/config/http.js";
export default {
  save () {
    let param = new URLSearchParams();
    axios
      .post('/pc/ship/findCurrency', param)
      .then(res => {
        if (res.data.code == 0) {
          var obj = JSON.stringify(res.data.data)
          localStorage.setItem('currency', obj)
        }
      })
  },
  getMoney (number, n) {
    n = n ? parseInt(n) : 8;
    if (n <= 0) return Math.round(number);
    number = Math.round(number * Math.pow(10, n)) / Math.pow(10, n);
    return number;
  },
  /**
   * @desc 例如后台给传bondpriceen:"130#122,127#1221"
   * @param listarr 原字符串
   * @param currencyData  用来循环哪个值是什么货币，人民币、美元、欧元、港币
   * @param lang 中英
   * @returns {*[]}
   */
  currencyType (listarr, currencyData, lang) {
    var listarrNew = [];
    var str = [];
    listarr = listarr.split(",")
    for (let i in listarr) {
      var obj = listarr[i].split('#')[0];
      for (let j in currencyData) {
        if (obj == currencyData[j].shipcoinclassificationid) {
          if (lang == 'Zh') {
            listarrNew.push(currencyData[j].nameen + '' + listarr[i].split('#')[1] + '万');
          }
          if (lang == 'En') {
            var arr = listarr[i].split('#')[1] + ''
            arr = arr.split('.');
            if (arr.length == 2) {
              let zhengshu = arr[0];  //获取整数部分
              let xiaoshu = arr[1];   //获取小数部分
              var num
              if (xiaoshu.length == 2) {
                let value = (zhengshu + xiaoshu) * 100;
                num = value.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');

              }else if (xiaoshu.length == 3) {
                let value = (zhengshu + xiaoshu) * 10;
                num = value.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');

              }else if (xiaoshu.length == 4) {
                let value = (zhengshu + xiaoshu) * 1;
                num = value.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
              }
               else {
                num = (zhengshu + xiaoshu) * 1000;
              }
              listarrNew.push(currencyData[j].name + '' + num);
            } else {
              num = listarr[i].split('#')[1] * 10000;
              listarrNew.push(currencyData[j].name + '' + ((num.toString().indexOf('.') !== -1) ? num.toLocaleString() : num.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')));
            }
          }
        }
      }
    }
    for (let u in listarrNew) {
      if (lang == 'Zh') {
        str += listarrNew[u] + ' 或 '
      }
      if (lang == 'En') {
        str += listarrNew[u] + ' or '
      }
    }
    if (str.length > 0) {
      str = str.substr(0, str.length - 3);
    }
    return str;
  }
}
